﻿Shader "Custom/Terrain3" {
	Properties {
		[HideInInspector] _Control ("Control (RGBA)", 2D) = "red" {}
		[HideInInspector] _Splat3 ("Layer 3 (A)", 2D) = "white" {}
		[HideInInspector] _Splat2 ("Layer 2 (B)", 2D) = "white" {}
		[HideInInspector] _Splat1 ("Layer 1 (G)", 2D) = "white" {}
		[HideInInspector] _Splat0 ("Layer 0 (R)", 2D) = "white" {}
		// used in fallback on old cards & base map
		// _MainTex ("BaseMap (RGB)", 2D) = "white" {}
		[HideInInspector] _Color ("Main Color", Color) = (1,1,1,1)
	}

		CGINCLUDE

		#pragma surface surf Lambert vertex:SplatmapVert finalcolor:SplatmapFinalColor
		//#pragma multi_compile_fog
		#include "TerrainSplatmapCommon.cginc"
		sampler2D _FuckTex; 


		fixed4 splatAlphaBlend(fixed4 current, sampler2D img, fixed2 uv, fixed alpha, fixed fixCol) {
			fixed4 imgCol = tex2D(img, uv);
			//fixed realAlpha = alpha * imgCol.a;
			fixed realAlpha = alpha;
			return ((1-realAlpha*imgCol.a) * current  + realAlpha * imgCol*fixCol) * (1) ;
		}

		fixed4 alphaOutline(fixed alpha, fixed4 baseColor, fixed4 outlineColor, fixed4 current) {
			//0.8 ~ 0.9  0.8~0.82   lerp
			//alpha 1-0.8 edge  0-1 ---> 1-0
			// baseColor.a 
			const fixed low = 0.155;
			const fixed lowUp = 0.8;
			const fixed hiLow = 0.85;
			const fixed hi = 0.2;
			const fixed bl = 0.7;

			fixed factor = step(low, alpha) * step(alpha, hi);
			/*
			fixed whichInt = step(alpha, lowUp); 
			fixed4 sc1 = lerp(baseColor, outlineColor, smoothstep(low, lowUp, alpha)) * whichInt;
			fixed4 sc2 = lerp(outlineColor, baseColor, smoothstep(hiLow, hi, alpha)) * (1-whichInt);
			*/
			//return factor * (sc1 + sc2) + (1-factor) * baseColor;

			return factor * ((1-bl)*current + outlineColor) + (1-factor) *((1-alpha*baseColor.a)*current + alpha * baseColor ) ;
		}

		fixed4 splatOutlineBlend(fixed4 current, sampler2D decalMap, fixed2 uv, fixed alpha, fixed4 outlineColor) {
			fixed4 col = tex2D(decalMap, uv);
			fixed4 outlined = alphaOutline(alpha, col, outlineColor, current);
			//return (1-alpha*outlined.a) * current + alpha * outlined;
			return outlined;
		}

		void SplatmapMix2(Input IN, out half4 splat_control, out half weight, out fixed4 mixedDiffuse)
		{
			splat_control = tex2D(_Control, IN.tc_Control);
			weight = dot(splat_control, half4(1,1,1,1));

			// Normalize weights before lighting and restore weights in final modifier functions so that the overal
			// lighting result can be correctly weighted.
			splat_control /= (weight + 1e-3f);

			/*
			mixedDiffuse = 0.0f;
			mixedDiffuse += splat_control.r * tex2D(_Splat0, IN.uv_Splat0);
			mixedDiffuse += splat_control.g * tex2D(_Splat1, IN.uv_Splat1);
			mixedDiffuse += splat_control.b * tex2D(_Splat2, IN.uv_Splat2);
			mixedDiffuse += splat_control.a * tex2D(_Splat3, IN.uv_Splat3);
			*/

			//fixed4 mainCol = tex2D(_MainTex, fixed2(0.5, 0.5));
			//mixedDiffuse = mainCol;

			fixed4 mainCol = tex2D(_FuckTex, IN.tc_Control);
			mixedDiffuse = mainCol;

			//fixed4 imgCol = tex2D(_Splat0, IN.uv_Splat0);

			/*
			//mixedDiffuse = (1-splat_control.r) * mixedDiffuse +  imgCol.a * splat_control.r * imgCol ; 
			mixedDiffuse = splatAlphaBlend(mixedDiffuse, _Splat0, IN.uv_Splat0, splat_control.r);
			mixedDiffuse = splatAlphaBlend(mixedDiffuse, _Splat1, IN.uv_Splat1, splat_control.g);
			mixedDiffuse = splatAlphaBlend(mixedDiffuse, _Splat2, IN.uv_Splat2, splat_control.b);
			mixedDiffuse = splatAlphaBlend(mixedDiffuse, _Splat3, IN.uv_Splat3, splat_control.a);
			*/

			const fixed4 outlineCol = fixed4(0, 0, 0, 1); 
			mixedDiffuse = splatAlphaBlend(mixedDiffuse, _Splat0, IN.uv_Splat0, splat_control.r, 0.6);
			mixedDiffuse = splatAlphaBlend(mixedDiffuse, _Splat1, IN.uv_Splat1, splat_control.g, 1);

			//mixedDiffuse = splatOutlineBlend(mixedDiffuse, _Splat2, IN.uv_Splat2, splat_control.b, outlineCol);
			//mixedDiffuse = splatOutlineBlend(mixedDiffuse, _Splat3, IN.uv_Splat3, splat_control.a, outlineCol);


			mixedDiffuse = splatAlphaBlend(mixedDiffuse, _Splat2, IN.uv_Splat2, splat_control.b, 1);
			mixedDiffuse = splatAlphaBlend(mixedDiffuse, _Splat3, IN.uv_Splat3, splat_control.a, 1);

		}

		void surf(Input IN, inout SurfaceOutput o)
		{
			half4 splat_control;
			half weight;
			fixed4 mixedDiffuse;
			SplatmapMix2(IN, splat_control, weight, mixedDiffuse);

			o.Albedo = mixedDiffuse.rgb;
			//o.Alpha = weight;
			o.Alpha = 1;
		}

	ENDCG

	Category {
		Tags {
			"Queue" = "Geometry-99"
			"RenderType" = "Opaque"
		}
		//Blend SrcAlpha OneMinusSrcAlpha

		// TODO: Seems like "#pragma target 3.0 _TERRAIN_NORMAL_MAP" can't fallback correctly on less capable devices?
		// Use two sub-shaders to simulate different features for different targets and still fallback correctly.
		SubShader { // for sm3.0+ targets
			CGPROGRAM
				#pragma target 3.0
				//#pragma multi_compile __ _TERRAIN_NORMAL_MAP
			ENDCG
		}
		SubShader { // for sm2.0 targets
			CGPROGRAM

			ENDCG
		}
	}

	Dependency "AddPassShader" = "Hidden/TerrainEngine/Splatmap/Diffuse-AddPass"
	Dependency "BaseMapShader" = "Diffuse"
	Dependency "Details0"      = "Hidden/TerrainEngine/Details/Vertexlit"
	Dependency "Details1"      = "Hidden/TerrainEngine/Details/WavingDoublePass"
	Dependency "Details2"      = "Hidden/TerrainEngine/Details/BillboardWavingDoublePass"
	Dependency "Tree0"         = "Hidden/TerrainEngine/BillboardTree"

	Fallback "Diffuse"
}
